package com.aifast.common.dict;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.aifast.common.cache.util.RedisUtil;
import com.aifast.common.global.Const;
import com.jfinal.plugin.activerecord.Db;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @Author fujian
 * @since 2022/9/9 11:40
 * 字典转译器 具体实现
 */
@Component
public class DictBindTranslator {

//    @Autowired
//    private SysDictDataService service;
    @Autowired
    private IDictBindService service;
    private static DictBindTranslator translator;
    private final static String suffix = "Str";

    @PostConstruct
    public void init() {
        translator = this;
        translator.service = this.service;
    }

    public static String genTargetFieldName(String currentName, String target) {
        return StrUtil.isBlank(target) ? StrUtil.join(Const.EMPTY, currentName, suffix) : target;
    }

    public static Object genTargetFieldValue(DictBind dictBind, Object value) {
        String result = null;
        String type = dictBind.type();

        DictBindType mode = dictBind.mode();

        switch (mode) {
            case DICT:
                if (StrUtil.isBlank(type)) {
                    result = Const.EMPTY;
                    break;
                }
                if(StrUtil.isBlank(dictBind.joinSymbol())){
                    result = translator.service.getDictDataValue(type, value);
                }else {
                    String[] strings = Convert.toStr(value).split(dictBind.joinSymbol());
                    result = getDictDataValue(dictBind.type(), strings,dictBind.joinSymbol());
                }
                break;
            case JOIN:
                if (StrUtil.isBlank(dictBind.joinSymbol())) {
                    result = getTableDataValue(dictBind.tableName(), dictBind.columns(), value);
                } else {
                    String[] strings = Convert.toStr(value).split(dictBind.joinSymbol());
                    result = getTableDataValue(dictBind, strings);
                }
                break;
        }

        return result;
    }

    public static String getDictDataValue(String type,String[] strings,String joinSymbol){
        return Arrays.stream(strings).map(x -> {
            return translator.service.getDictDataValue(type,x);
        }).collect(Collectors.joining(joinSymbol));
    }

    private static String getTableDataValue(DictBind dictBind, String[] strings) {
        return Arrays.stream(strings).map(x -> {
            return getTableDataValue(dictBind.tableName(), dictBind.columns(), x);
        }).collect(Collectors.joining(dictBind.joinSymbol()));
    }

    private static String getTableDataValue(String tableName, String columns, Object dataId) {
        String dictTableKey = RedisUtil.getDictTableKey(tableName);
        if (RedisUtil.hexists(dictTableKey, Convert.toStr(dataId))) {
            return (String) RedisUtil.hget(dictTableKey, Convert.toStr(dataId));
        }
        String sql = StrUtil.format("select {} from {} where id = {}", columns, tableName, dataId);
        String str = Db.queryStr(sql);
        RedisUtil.hset(dictTableKey, dataId.toString(), str);
        return str;
    }


}
